﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//application stuff
namespace gwBedrockApp
{
    //base class of application framework
    public class gwApplication
    {
        #region protected variable
        //whether opened
        protected bool m_bIsOpen          = false;
        //whether running
        protected bool m_bRunning         = false;
        //the return code when exit main()
        protected int  m_lReturnCode      = 0;
        //your company name
        protected string m_strCompanyName = "GW System";
        //your application name
        protected string m_strAppName     = "GW Engine Demo";
        //your application version
        protected string m_strAppVersion  = "1.00";
        //command line arguments parsed through gwCommandLine object
        protected gwCommandLine m_commandLineArgs;
        #endregion

        #region public variable
        //gloabl instance
        public static gwApplication Instance = null;
        #endregion

        #region public function
        /// <summary>
        /// your company name property
        /// </summary>
        public string CompanyName
        {
            get
            {
                return m_strCompanyName;
            }
            set
            {
               m_strCompanyName = value;
            }
        }
        /// <summary>
        /// your application name property
        /// </summary>
        public string AppName
        {
            get
            {
                return m_strAppName;
            }
            set
            {
                m_strAppName = value;
            }
        }
        /// <summary>
        /// your application version property
        /// </summary>
        public string AppVersion
        {
            get
            {
                return m_strAppVersion;
            }
            set
            {
                m_strAppVersion = value;
            }
        }
        /// <summary>
        /// command line arguments
        /// </summary>
        public gwCommandLine CommandLineArgs
        {
            get
            {
                return m_commandLineArgs;
            }
            set
            {
                m_commandLineArgs = value;
            }
        }
        /// <summary>
        /// whether opened
        /// </summary>
        /// <returns></returns>
        bool IsOpen
        {
            get
            {
                return m_bIsOpen;
            }
        }
        /// <summary>
        /// whether running
        /// </summary>
        /// <returns></returns>
        bool IsRunning
        {
            get
            {
                return m_bRunning;
            }
        }
        /// <summary>
        /// return code property
        /// </summary>
        int ReturnCode
        {
            get
            {
                return m_lReturnCode;
            }
            set
            {
                m_lReturnCode = value;
            }
        }

        /// <summary>
        /// terminate application
        /// </summary>
        public void Terminate()
        {
            m_bRunning = false;
        }
        #endregion

        #region virtual function
        /// <summary>
        /// entry point, can be override by user
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public virtual int Main(string[] args)
        {
            CommandLineArgs = new gwCommandLine(gwCommandLine.ConvertCmdArgToString(args));
            if (Instance.Open())
            {
                while (m_bRunning)
                {
                    Instance.Run();
                }

                Instance.Close();
            }

            return Instance.ReturnCode;
        }

        /// <summary>
        /// open application, can be override by user, do something initialize heres
        /// </summary>
        /// <returns></returns>
        protected virtual bool Open()
        {
            m_bIsOpen = true;
            m_bRunning = true;
            return m_bIsOpen;
        }
        /// <summary>
        /// close application, can be override by user, destroy globa object should be here
        /// </summary>
        protected virtual void Close()
        {
            m_bIsOpen = false;
        }
        /// <summary>
        /// application loop, can be override by user, implement their own application loop
        /// </summary>
        protected virtual void Run()
        {
            //do nothing, override this function as you wish
        }
        #endregion
    }

    public partial class gwApplicationCreator
    {

    }
}


class Program
{
    static int Main(string[] args)
    {
        gwBedrockApp.gwApplicationCreator initApp = new gwBedrockApp.gwApplicationCreator();
        int returnCode = 0;
        if (gwBedrockApp.gwApplication.Instance != null)
        {
            returnCode = gwBedrockApp.gwApplication.Instance.Main(args);
        }

        return returnCode;
    }
}
